package com.pub.functions;

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class LinuxCommand {

	public static void ZeewaveInstall(String path) {
		Process process = null;
		OutputStream out = null;
		InputStream in = null;
		try {

			process = Runtime.getRuntime().exec("su" + "\n");
			DataOutputStream os = null;
			os = new DataOutputStream(process.getOutputStream());
			out = process.getOutputStream();
			os.writeBytes("chmod 777 " + path + "\n");
			os.flush();

			os.writeBytes("pm install -r " + path + "\n");
			os.flush();

		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (out != null) {
					out.flush();
					out.close();
				}
				if (in != null) {
					in.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public static void ZeewaveMkdir(String path) {
		Process process = null;
		OutputStream out = null;
		InputStream in = null;
		try {
			process = Runtime.getRuntime().exec("/system/xbin/su" + "\n");
			DataOutputStream os = null;
			os = new DataOutputStream(process.getOutputStream());
			out = process.getOutputStream();

			os.writeBytes("busybox-1.9.0-armv6l mkdir " + path + "\n");
			os.flush();
			os.writeBytes("busybox-1.9.0-armv6l chmod 777 " + path + "\n");
			os.flush();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (out != null) {
					out.flush();
					out.close();
				}
				if (in != null) {
					in.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public static void ZeewaveMv(String path) {
		Process process = null;
		OutputStream out = null;
		InputStream in = null;
		try {
			process = Runtime.getRuntime().exec("/system/xbin/su" + "\n");
			DataOutputStream os = null;
			os = new DataOutputStream(process.getOutputStream());
			out = process.getOutputStream();

			os.writeBytes("busybox-1.9.0-armv6l mv " + path
					+ " /data/zeewaveapp/" + "\n");
			os.flush();
			os.writeBytes("busybox-1.9.0-armv6l chmod 777 " + path + "\n");
			os.flush();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (out != null) {
					out.flush();
					out.close();
				}
				if (in != null) {
					in.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public static void ZeewaveCp(String srcPath, String desPath) {
		Process process = null;
		OutputStream out = null;
		InputStream in = null;

		try {
			process = Runtime.getRuntime().exec("/system/xbin/su" + "\n");
			DataOutputStream os = null;
			os = new DataOutputStream(process.getOutputStream());
			out = process.getOutputStream();

			os.writeBytes("busybox-1.9.0-armv6l cp " + srcPath + " " + desPath
					+ "\n");
			os.flush();
			os.writeBytes("busybox-1.9.0-armv6l chmod 777 " + desPath + "\n");
			os.flush();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (out != null) {
					out.flush();
					out.close();
				}
				if (in != null) {
					in.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public static void ZeewaveSetStaticIp(String ip, String gateway, String mask) {
		Process process = null;
		OutputStream out = null;
		InputStream in = null;
		try {
			process = Runtime.getRuntime().exec("su" + "\n");
			DataOutputStream os = null;
			os = new DataOutputStream(process.getOutputStream());
			out = process.getOutputStream();

			os.writeBytes("busybox-1.9.0-armv6l ifconfig eth0 " + ip
					+ " netmask " + mask + " up" + "\n");
			os.flush();
			os.writeBytes("busybox-1.9.0-armv6l route add default gw "
					+ gateway + "\n");
			os.flush();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (out != null) {
					out.flush();
					out.close();
				}
				if (in != null) {
					in.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public static void ZeewaveChmod(String path) {
		Process process = null;
		OutputStream out = null;
		InputStream in = null;
		try {
			process = Runtime.getRuntime().exec("su" + "\n");
			DataOutputStream os = null;
			os = new DataOutputStream(process.getOutputStream());
			out = process.getOutputStream();

			os.writeBytes("chmod 777 " + path + "\n");
			os.flush();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (out != null) {
					out.flush();
					out.close();
				}
				if (in != null) {
					in.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public static void ZeewaveReboot() {
		Process process = null;
		OutputStream out = null;
		InputStream in = null;
		try {
			process = Runtime.getRuntime().exec("su" + "\n");
			DataOutputStream os = null;
			os = new DataOutputStream(process.getOutputStream());
			out = process.getOutputStream();

			os.writeBytes("sync" + "\n");
			os.flush();
			os.writeBytes("reboot" + "\n");
			os.flush();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (out != null) {
					out.flush();
					out.close();
				}
				if (in != null) {
					in.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public static void ZeewaveSetRoute() {
		Process process = null;
		OutputStream out = null;
		InputStream in = null;
		try {

			process = Runtime.getRuntime().exec("su" + "\n");
			DataOutputStream os = null;
			os = new DataOutputStream(process.getOutputStream());
			out = process.getOutputStream();

			os.writeBytes("busybox-1.9.0-armv6l route add default gw 192.168.43.1"
					+ "\n");
			os.flush();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (out != null) {
					out.flush();
					out.close();
				}
				if (in != null) {
					in.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public static void ZeewaveResetNetwork(String cmd) {
		Process process = null;
		OutputStream out = null;
		InputStream in = null;
		try {

			process = Runtime.getRuntime().exec("su" + "\n");
			DataOutputStream os = null;
			os = new DataOutputStream(process.getOutputStream());
			out = process.getOutputStream();

			os.writeBytes(cmd + "\n");
			os.flush();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (out != null) {
					out.flush();
					out.close();
				}
				if (in != null) {
					in.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
